package www.dashu.com.controller;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
import www.dashu.com.api.CommonResult;
import www.dashu.com.domain.CProductAttributeValue;
import www.dashu.com.service.CProductAttributeValueService;
import www.dashu.com.service.RedisService;
import www.dashu.com.domain.CProductAttribute;
import www.dashu.com.dto.CProductAttributeQuery;
import www.dashu.com.service.CProductAttributeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.lang.reflect.Field;
import java.util.*;

@Controller
@Api(tags = "CProductAttributeController", description = "商品属性参数表")
@RequestMapping("/productAttribute")
public class CProductAttributeController
{

    @Autowired
    CProductAttributeService productAttributeService;
    @Autowired
    RedisService redisService;
    @Autowired
    CProductAttributeValueService attributeValueService;

    @ApiOperation(value = "商品属性参数表列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<Page<CProductAttribute>> list(CProductAttributeQuery query) {

        String productId = query.getProductId();
        query.setProductId(null);
        try {
            Page<CProductAttribute> pageData = productAttributeService.findAll(query);

            pageData.getContent().forEach(attribute -> {
                attribute.setValues(new ArrayList<>());
                attribute.setOptions(new ArrayList<>());
                if(!StringUtils.isEmpty(productId)) {
                    Map<String, Object> queryMap = new HashMap<>();
                    queryMap.put("productId", productId);
                    queryMap.put("productAttributeId", attribute.getId());
                    List<CProductAttributeValue> attributeValueList = attributeValueService.findAll(queryMap);
                    if (null != attributeValueList) {
                        attributeValueList.forEach(attributeValue -> {
                            if (!StringUtils.isEmpty(attributeValue.getValue())) {
                                attribute.setValues(Arrays.asList(attributeValue.getValue().split(",")));
                            }
                            if (!StringUtils.isEmpty(attributeValue.getOprations())) {
                                attribute.setOptions(JSON.parseArray(attributeValue.getOprations()));
                            }

                        });
                    }
                }
            });

            return CommonResult.success(pageData);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    @ApiOperation(value = "新增商品属性参数表")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult create(@RequestBody CProductAttribute productAttribute) {
        productAttribute.setId(productAttributeService.createId(productAttribute));

        Field[] fields = productAttribute.getClass().getDeclaredFields();
        for(Field field : fields){
            field.setAccessible(true);
            if(field.getName().toLowerCase().contains("time")){
                try {
                    field.set(productAttribute, new Date());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        productAttributeService.save(productAttribute);
        return CommonResult.success();
    }

    @ApiOperation(value = "修改商品属性参数表")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult update(@RequestBody CProductAttribute productAttribute) {

        Field[] fields = productAttribute.getClass().getDeclaredFields();
        for(Field field : fields){
            field.setAccessible(true);
            if(field.getName().toLowerCase().contains("updatetime")){
                try {
                    field.set(productAttribute, new Date());
                 } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        productAttributeService.save(productAttribute);
        return CommonResult.success();
    }

    @ApiOperation(value = "删除商品属性参数表")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    @Transactional(propagation= Propagation.REQUIRED, rollbackFor = Exception.class)
    public CommonResult delete(@RequestBody CProductAttribute productAttribute) {
        productAttributeService.deleteById(productAttribute.getId());
        return CommonResult.success();
    }

}
